package com.neu.his.controller.SystemManage01.UserManage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neu.his.constrant.GlobalConstrant;
import com.neu.his.core.Result;
import com.neu.his.entity.Menu;
import com.neu.his.entity.RoleMenu;
import com.neu.his.entity.User;
import com.neu.his.entity.UserRole;
import com.neu.his.entity.VO.SystemManage01.UserManage.TokenVO;
import com.neu.his.entity.request.UserManage.LoginParams;
import com.neu.his.mapper.MenuMapper;
import com.neu.his.mapper.RoleMenuMapper;
import com.neu.his.mapper.UserMapper;
import com.neu.his.mapper.UserRoleMapper;
import com.neu.his.service.UserService;
import com.neu.his.utils.JwtUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/17 14:43
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Autowired
    MenuMapper menuMapper;

    @PostMapping
    public Result login(@RequestBody LoginParams loginParams) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name", loginParams.getUsername());
        queryWrapper.eq("pass_word", loginParams.getPassword());
        queryWrapper.ne("remove_mark",1L);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            // 返回到客户端一个token
            String token = JwtUtils.sign(loginParams.getUsername(), GlobalConstrant.TOKEN_SECRET);
            TokenVO tokenVO = new TokenVO();
            List<Menu> menus = new ArrayList<Menu>();
            tokenVO.setToken(token);
            tokenVO.setUserId(user.getId());
            List<UserRole> userRoles = userRoleMapper.selectList(new QueryWrapper<UserRole>().ne("remove_mark", 1L).eq("user_id", user.getId()));
            List<Long> roleIds = new ArrayList<Long>();
            for (UserRole userRole : userRoles) roleIds.add(userRole.getRoleId());
            List<RoleMenu> roleMenus = roleMenuMapper.selectList(new QueryWrapper<RoleMenu>().ne("remove_mark", 1L).in("role_id", roleIds));
            List<Long> menuids = new ArrayList<Long>();
            for (RoleMenu roleMenu : roleMenus) menuids.add(roleMenu.getMenuId());
            menus = menuMapper.selectList(new QueryWrapper<Menu>().ne("remove_mark", 1L).in("id", menuids));
            tokenVO.setResult(menus);
            return Result.success(tokenVO);
        }
        return Result.fail("登录失败");
    }
}
